package com.example.yyy

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationCompat
import org.jetbrains.anko.intentFor

class SendStyleNotify(context: Context, title: String, message: String,type: Int) {

    var style:NotificationCompat.Style? = null

    init {
        when(type){
            0 -> {
                //大文本风格
                style = NotificationCompat.BigTextStyle()
                    .setBigContentTitle(title)
                    .setSummaryText(message)
                    .bigText("这是一条大文字风格的通知消息")
            }
            1 -> {
                //大图片风格
                style = NotificationCompat.BigPictureStyle()
                    .setBigContentTitle(title)
                    .setSummaryText(message)
                    .bigPicture(BitmapFactory.decodeResource(context.resources, R.drawable.tp))
                    .bigLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_launcher_foreground))
            }
            2 ->{
                //收件箱风格
                style = NotificationCompat.InboxStyle()
                    .setBigContentTitle(title)
                    .setSummaryText(message)
                    .addLine("他是的空间打开来看")
                    .addLine("他是的空间打开来看")
                    .addLine("他是的空间打开来看")
            }
        }
        //声明一个点击通知消息时触发的动作意图
        val clickIntent = context.intentFor<MainActivity>()
        val intent = PendingIntent.getActivity(
            context,
            R.string.app_name,
            clickIntent,
            PendingIntent.FLAG_CANCEL_CURRENT
        )
        //构建消息参数
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, "CHANNELID")
        builder.setContentIntent(intent)
            .setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSubText("这是副本")
            .setTicker("简单消息")
            .setWhen(System.currentTimeMillis())
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.tp))
            .setContentTitle(title)
            .setContentText(message)
        builder.setStyle(style)
        //获取通知管理器
        val manager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("3", "channel3", NotificationManager.IMPORTANCE_DEFAULT)
            channel.enableLights(true)
            channel.lightColor = Color.green(3)
            channel.setShowBadge(true)
            manager.createNotificationChannel(channel)
            builder.setChannelId("3")
        }
        val n = builder.build()
        manager.notify(3, n)
    }

}